package action.login;

import model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import service.LoginService;

import com.opensymphony.xwork2.ActionSupport;
@Namespace("")
@Action(
	value="login",
	results={
			@Result(name="admin",location="/WEB-INF/teacher.jsp"),
			@Result(name="student",location="/WEB-INF/student.jsp"),
			@Result(name="input",location="/index.jsp")
	}		
)
public class Login extends ActionSupport {
	private String user_id;
	private String password;
	public String execute() throws Exception{
		User user=new User();
		user.setUser_id(user_id);
		user.setPassword(password);
		LoginService ls=new LoginService();
		int flag=ls.checkUser(user);
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(flag==1){
			user.setRole_id(flag);
			session.setAttribute("user_id", user_id);
			session.setAttribute("username", user.getUsername());
			return "admin";
		}
		else if(flag==2){
			user.setRole_id(flag);
			session.setAttribute("user_id", user_id);
			session.setAttribute("username", user.getUsername());
			return "student";
		}
		request.setAttribute("loginmsg", "<script>alert('用户名或密码错误')</script>");
		return INPUT;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
}
